package edu.uestc.canary.admin.web.controller;

import edu.uestc.canary.commons.files.service.FileService;
import edu.uestc.canary.entity.dto.BookParams;
import edu.uestc.canary.entity.dto.Result;
import edu.uestc.canary.entity.pojo.Book;
import edu.uestc.canary.service.BookService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 图书管理api接口
 */
@RestController
@RequestMapping("/books")
@Tag(name = "Book", description = "图书管理api接口")
public class BookManagerController {
    @Resource private BookService service;
    @Resource private FileService fileService;

    /**
     * 图书分页列表
     */
    @GetMapping
    @Operation(description = "图书分页列表")
    public Result pagination(BookParams bookParams){
        return service.pagination(bookParams);
    }

    @PostMapping("/cover")
    public Result fileUpload(MultipartFile file) throws IOException {
        System.out.println(file);
        if(file != null && !file.isEmpty()) {
            var path = fileService.uploadImgFile(file.getContentType(), file.getOriginalFilename(), file.getInputStream());
            return Result.ok(path, "图书封面上传成功");
        }
        return Result.fail();
    }

    @PostMapping
    public Result save(@RequestBody Book book){
        service.save(book);
        return Result.ok();
    }
}
